<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu = array();

	public $active_menu = 'home';
	
	public $sidebar = '//sidebars/sidebar';
	
	public $seo_data = array();
	
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();

	public $categories = array();
	public $columns_per_row = 4;

	public $result_ids = array();
	protected $_result = array();

	public $is_home = false;
	
	public function init() {
		$this->result_ids = getParam('result_ids') ? explode(',', getParam('result_ids')) : array();

		// main menu
		$main_menus = array(
			'home' => array(
				'label' => 'Home',
				'type' => 'link', 
				'mobile_index' => 1,
				'url' => bu('/'),
			),
			'user' => array(
				'label' => 'Find Friend',
				'type' => 'link',
				'url' => bu('/search-user.html'),
				'mobile_index' => 2,
			),
			'register' => array(
				'label' => 'Register',
				'type' => 'link',
				'url' => bu('/register-account.html'),
				'mobile_index' => 3
			)
		);
		if(!user()->isGuest)
		{
			$main_menus = array(
				'home' => array(
					'label' => 'Home',
					'type' => 'link', 
					'mobile_index' => 1,
					'url' => bu('/'),
				),
				'user' => array(
					'label' => 'Find Friend',
					'type' => 'link',
					'url' => bu('/search-user.html'),
					'mobile_index' => 2,
				),
				'admin' => array(
					'label' => 'Admin Page',
					'type' => 'link',
					'url' => bu('/admin-page.html'),
					'mobile_index' => 3
				)
			);
		}
		$this->menu = $main_menus;
		
		$active_menu = getQuery('active_menu');
		if(!empty($active_menu))
		{
			$this->active_menu = $active_menu;
		}
	}
	
	public function getBodyClasses() {
		
		if (!empty(Yii::app()->baseUrl)) {
			$uri = explode(Yii::app()->baseUrl, Yii::app()->request->requestUri);
			unset($uri[0]);
			$components = explode("/", implode(Yii::app()->baseUrl, $uri));
		} else {
			$components = explode("/", Yii::app()->request->requestUri);
		}

		foreach ($components as $id => $component) {
			if (empty($component)) {
				unset($components[$id]);
			}
		}
		ksort($components);

		$class = '';
		for ($x = 1; $x <= sizeOf($components); $x++) {
			if ($x <> 1) {
				$class .= '-';
			}
			$class .= $components[$x];
			$classes[] = 'body-'.strip_tags($class);
		}
		
		return !empty($classes) ? implode(' ', $classes) : 'body-home';
	}

	public function filters()
    {
        return array(
            //'accessControl',
        );
    }
	
	public function loginRequired($redirect = false) {
		if ( user()->isGuest ) {
			if (isAjaxRequest() ) {
				if ( $redirect ) {
					$this->assignVar('force_redirection', $this->createUrl('/user/login'));
				} else {
					$this->assignVar('login_required', true);
				}
				$this->ajaxResponse('');
				app()->end();
			} else {
				$this->redirect($this->createUrl('/user/login'));
			}
		}
	}

    public function accessRules()
    {
        return array( 
        );
    }

	public function pageTitle($title, $override = false) {
		if ( $override ) {
			$pageTitle = sprintf('%s', $title);
		} else {
			$pageTitle = sprintf('%s - %s', $title, app()->name);
		}

		$this->pageTitle = $pageTitle;
		return $pageTitle;
	}

	public function getOutput($view, $data = array()) {
		return $this->renderPartial($view, $data, true);
	}

	public function renderAjax($view, $data = array()) {
		$html = $this->getOutPut($view, $data);

		$this->ajaxResponse($html);
	}

	public function ajaxResponse($html) {
		$text = $html;

		if ( !empty($this->result_ids) ) {
			foreach ($this->result_ids as $r_id) {
				if (strpos($text, ' id="' . $r_id . '">') !== false) {
					$start = strpos($text, ' id="'.$r_id.'">') + strlen(' id="' . $r_id . '">');
					$end = strpos($text, '<!--' . $r_id . '--></');
					$this->assignHtml($r_id, substr($text, $start, $end - $start));
				// Assume that all data should be put to div with this ID
				}
			}
			$text = '';
		}

		$notifications = user()->getNotifications();
		if ( !empty($notifications) ) {
			$this->assignVar('notifications', $notifications);
		}

		session_write_close();

		$response = CJSON::encode(array(
			'text' => trim($text),
			'data' => $this->_result,
		));

		echo $response;

		app()->end();
	}

	public function assignHtml($r_id, $text) {
		$this->_result['html'][$r_id] = $text;
	}

	public function assignVar($var, $value) {
		$this->_result[$var] = $value;
	}

	public function createIdUrl($route, $params = array(), $ampersand = '&') {
		$uri = parent::createUrl($route, $params, $ampersand);
		$idUrl = param('idUrl');

		return $idUrl . $uri;
	}
}